package com.yj.suncity.activity;

import org.json.JSONObject;

import com.yj.suncity.R;
import com.yj.suncity.async.ThreadUtils;
import com.yj.suncity.async.ThreadUtils.OnResultListener;
import com.yj.suncity.collect.CollectActivityListActivity;
import com.yj.suncity.collect.CollectAdvListActivity;
import com.yj.suncity.collect.CollectDigouListActivity;
import com.yj.suncity.collect.CollectDuihuanListActivity;
import com.yj.suncity.collect.CollectHelpListActivity;
import com.yj.suncity.collect.CollectOfferListActivity;
import com.yj.suncity.collect.CollectShopListActivity;
import com.yj.suncity.collect.bean.CollectNumBean;
import com.yj.suncity.constant.ShareCon;
import com.yj.suncity.constant.URLCon;
import com.yj.suncity.user.LoginActivity;
import com.yj.suncity.utis.SharedPreferencesUtil;
import com.yj.suncity.utis.ToastUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class CollectActivity extends Activity {
	private Context context;
	private SharedPreferencesUtil preferencesUtil = SharedPreferencesUtil
			.getInstance();
	private boolean isLogin;
	private String userId;
	private TextView mTxt_adv_num, mTxt_offer_num, mTxt_cate_num,
			mTxt_help_num, mTxt_shangjia_num, mTxt_digou_num, mTxt_activity_num,
			mTxt_shop_num, mTxt_bianmin_num;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_collect);
		context = this;
		findView();
	}

	private void findView() {
		mTxt_adv_num = (TextView) findViewById(R.id.txt_adv_num);
		mTxt_offer_num = (TextView) findViewById(R.id.txt_offer_num);
		mTxt_cate_num = (TextView) findViewById(R.id.txt_cate_num);
		mTxt_help_num = (TextView) findViewById(R.id.txt_help_num);
		mTxt_shangjia_num = (TextView) findViewById(R.id.txt_duihuan_num);
		mTxt_digou_num = (TextView) findViewById(R.id.txt_digou_num);
		mTxt_activity_num = (TextView) findViewById(R.id.txt_activity_num);
		mTxt_shop_num = (TextView) findViewById(R.id.txt_shop_num);
		mTxt_bianmin_num = (TextView) findViewById(R.id.txt_bianmin_num);

	}

	@Override
	protected void onResume() {
		isLogin = preferencesUtil.getBoolean(context, ShareCon.USERFILE,
				ShareCon.ISLOGIN, false);
		if (isLogin) {
			userId = preferencesUtil.getString(context, ShareCon.USERFILE,
					ShareCon.USERID, "");
			initData();
		} else {
			userId = "";
		}
		super.onResume();
	}

	private void initData() {
		String path = URLCon.收藏 + "?action=gettypecollectionnum&userid="
				+ userId;
		Log.d("", path);
		ThreadUtils threadUtils = new ThreadUtils();
		threadUtils.startThread(path, new OnResultListener() {
			@Override
			public void jsonResult(JSONObject json) {
				CollectNumBean numBean=new CollectNumBean(json);
				 if (!numBean.getEnt().equals("0")) {
		             	mTxt_cate_num.setVisibility(View.VISIBLE);
		             	mTxt_cate_num.setText(numBean.getEnt());
						}else{
							mTxt_cate_num.setVisibility(View.GONE);
						}
		             if (!numBean.getCityhelp().equals("0")) {
		             	mTxt_help_num.setVisibility(View.VISIBLE);
		             	mTxt_help_num.setText(numBean.getCityhelp());
						}else{
							mTxt_help_num.setVisibility(View.GONE);
						}
		             if (!numBean.getAdv().equals("0")) {
		             	mTxt_adv_num.setVisibility(View.VISIBLE);
		             	mTxt_adv_num.setText(numBean.getAdv());
						}else{
							mTxt_adv_num.setVisibility(View.GONE);
						}
		             if (!numBean.getOffer().equals("0")) {
		             	mTxt_offer_num.setVisibility(View.VISIBLE);
		             	mTxt_offer_num.setText(numBean.getOffer());
						}else{
							mTxt_offer_num.setVisibility(View.GONE);
						}
		             if (!numBean.getGoods().equals("0")) {
		            	 mTxt_shangjia_num.setVisibility(View.VISIBLE);
		            	 mTxt_shangjia_num.setText(numBean.getGoods());
						}else{
							mTxt_shangjia_num.setVisibility(View.GONE);
						}
		             if (!numBean.getDgfoods().equals("0")) {
		             	mTxt_digou_num.setVisibility(View.VISIBLE);
		             	mTxt_digou_num.setText(numBean.getDgfoods());
						}else{
							mTxt_digou_num.setVisibility(View.GONE);
						}
		             if (!numBean.getActivity().equals("0")) {
		             	mTxt_activity_num.setVisibility(View.VISIBLE);
		             	mTxt_activity_num.setText(numBean.getActivity());
						}else{
							mTxt_activity_num.setVisibility(View.GONE);
						}
			}
		});
	}


	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lay_rela_1_1:
			if (isLogin) {
				Intent intent=new Intent(this, CollectAdvListActivity.class);
				intent.putExtra("userid", userId);
				intent.putExtra("flag", 1);
				startActivity(intent);
			}else{
				startLoginActivity();
			}

			break;
		case R.id.lay_rela_1_2:
			if (isLogin) {
				Intent intent=new Intent(this, CollectOfferListActivity.class);
				intent.putExtra("userid", userId);
				startActivity(intent);
			}else{
				startLoginActivity();
			}
			break;
		case R.id.lay_rela_1_3:
	         //分类信息
			if (isLogin) {
				Intent intent=new Intent(this, CollectAdvListActivity.class);
				intent.putExtra("userid", userId);
				intent.putExtra("flag", 2);
				startActivity(intent);
			}else{
				startLoginActivity();
			}
			break;
		case R.id.lay_rela_2_1:
			if (isLogin) {
				Intent intent=new Intent(this, CollectHelpListActivity.class);
				intent.putExtra("userid", userId);
				startActivity(intent);
			}else{
				startLoginActivity();
			}
			break;
		case R.id.lay_rela_2_2:
			if (isLogin) {
				//商家商品
				Intent intent=new Intent(this, CollectDuihuanListActivity.class);
				intent.putExtra("userid", userId);
				startActivity(intent);
			}else{
				startLoginActivity();
			}
			break;
		case R.id.lay_rela_2_3:
			if (isLogin) {
				Intent intent=new Intent(this, CollectDigouListActivity.class);
				intent.putExtra("userid", userId);
				startActivity(intent);
			}else{
				startLoginActivity();
			}
			break;
		case R.id.lay_rela_3_1:
			if (isLogin) {
				Intent intent=new Intent(this, CollectActivityListActivity.class);
				intent.putExtra("userid", userId);
				startActivity(intent);
			}else{
				startLoginActivity();
			}
			break;
		case R.id.lay_rela_3_2://网购商城
//			if (isLogin) {
//				Intent intent=new Intent(this, CollectShopListActivity.class);
//				intent.putExtra("userid", userId);
//				startActivity(intent);
//			}else{
//				startLoginActivity();
//			}
			ToastUtil.ShowToast(context, "正在建设中......");
			break;
		case R.id.lay_rela_3_3:
               //便民服务
			ToastUtil.ShowToast(context, "正在建设中......");
			break;
		}
		
	}
	private void startLoginActivity(){
		startActivity(new Intent(context, LoginActivity.class));
	}

}
